package com.hksj.ecm.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.ecm.entity.EnergyRate;
import com.hksj.ecm.mapper.EnergyRateMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zengke
 * @version 1.0
 * @see EnergyRateService
 * @since 2024/8/8 16:21
 */

@Service
public class EnergyRateService extends ServiceImpl<EnergyRateMapper,EnergyRate> {

    /**
     * 新增能耗费率
     * @param energyRate
     * @return
     */

    @Transactional
    public String createEnergyRate(EnergyRate energyRate){

        //根据能源类型code查询该能源类型下的 费率列表
        List<EnergyRate> energyRateList = this.lambdaQuery()
                .eq(EnergyRate::getEnergyTypeCode, energyRate.getEnergyTypeCode())
                .list();

        //判断当前配置的费率范围填写是否正确
        if (!checkEnergyRate(energyRate,energyRateList)){
            throw new RuntimeException("费率范围填写有误");
        }

        //做新增
        super.save(energyRate);

        return "新增成功";
    }

    /**
     * 检查当前填写的费率范围是否正确
     * @param energyRate 传入的费率实体
     * @param energyRateList 数据库已存在的费率列表
     * @return true/false
     */
    private Boolean checkEnergyRate(EnergyRate energyRate,List<EnergyRate> energyRateList){

        //判断费率列表是否存在
        if (ObjectUtils.isEmpty(energyRateList)){

            //当前配置的费率列表不存在
            //判断范围起始值是否大于范围终止值
            if (new BigDecimal(energyRate.getStartRangeValue()).compareTo(new BigDecimal(energyRate.getEndRangeValue())) >= 0){
                //起始值大于终止值
                return false;
            }

        } else {
            //对费率列表进行排序根据 起始值大小正序排序
            energyRateList = energyRateList.stream().sorted((Comparator.comparing(o -> new BigDecimal(o.getStartRangeValue())))).collect(Collectors.toList());

            //当前费率列表存在 对当前的费率进行比较
            //取出当前列表的最后一条
            EnergyRate lastEnergyRate = energyRateList.stream().skip(energyRateList.size() - 1).findAny().orElse(new EnergyRate());

            //当前的起始值 需要大于最后一条的最大值
            if (new BigDecimal(energyRate.getStartRangeValue()).compareTo(new BigDecimal(lastEnergyRate.getEndRangeValue())) < 0){
                //当前值的最小值 小于 上一条记录的最大值
                return false;
            }

            //当前的起始值 需要小于当前终止值
            if (new BigDecimal(energyRate.getStartRangeValue()).compareTo(new BigDecimal(energyRate.getEndRangeValue())) >= 0){
                //起始值大于终止值
                return false;
            }
        }

        //以上检查都通过
        return true;
    }

    /**
     * 修改能耗费率
     * @param energyRate
     * @return
     */
    @Transactional
    public String updateEnergyRate(EnergyRate energyRate){

        //根据能源类型code查询该能源类型下的 费率列表
        List<EnergyRate> energyRateList = this.lambdaQuery()
                .eq(EnergyRate::getEnergyTypeCode, energyRate.getEnergyTypeCode()) //数据库字段energy_type_code为当前所传值energyTypeCode
                .ne(EnergyRate::getId,energyRate.getId())//数据库字段id 不为当前所传id
                .list();

        //判断当前配置的费率范围填写是否正确
        if (!checkEnergyRate(energyRate,energyRateList)){
            throw new RuntimeException("费率范围填写有误");
        }

        //做更新
        super.updateById(energyRate);

        return "更新成功";
    }

    /**
     * 删除能耗费率
     * @param id
     * @return
     */
    @Transactional
    public String deleteEnergyRate(String id){
        //做删除
        super.removeById(id);
        return "删除成功";
    }

    /**
     * 查询能耗费率列表
     */
    public List<EnergyRate> queryEnergyRateList(String energyTypeCode){
        //查询能耗费率列表
        return lambdaQuery().eq(EnergyRate::getEnergyTypeCode,energyTypeCode).list();
    }
}
